home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / DMAKE38B.ARJ / MEMORY.C < prev    next >
C/C++ Source or Header  |  1992-01-23  |  4KB  |  226 lines

  1. /*
  2.  * (c) Copyright 1990 Conor P. Cahill (uunet!virtech!cpcahil).  
  3.  * You may copy, distribute, and use this software as long as this
  4.  * copyright statement is not removed.
  5.  */
  6.  
  7. #ifndef lint
  8. static
  9. char rcs_hdr[] = "$Id: memory.c,v 1.1 1992/01/24 03:29:08 dvadura Exp $";
  10. #endif
  11.  
  12. void malloc_check_data();
  13.  
  14. char *
  15. memccpy(ptr1, ptr2, ch, len)
  16.     register char    * ptr1;
  17.     register char    * ptr2;
  18.     int          len;
  19.     int          ch;
  20. {
  21.     int          check;
  22.     register int      i;
  23.     char        * rtn;
  24.  
  25.     /*
  26.      * I know that the assignment could be done in the following, but
  27.      * I wanted to perform a check before any assignment, so first I 
  28.      * determine the length, check the pointers and then do the assignment.
  29.      */
  30.     for( i=0; (i < len) && (ptr2[i] != ch); i++)
  31.     {
  32.     }
  33.     if( ptr2[i] == ch )
  34.     {
  35.         check = i+1;
  36.     }
  37.     else
  38.     {
  39.         check = len;
  40.     }
  41.  
  42.     malloc_check_data("memccpy", ptr1, check);
  43.     malloc_check_data("memccpy", ptr2, check);
  44.  
  45.     /*
  46.      * if we found the character...
  47.       */
  48.  
  49.     if( i < len )
  50.     {
  51.         rtn = ptr1+i+1;
  52.         i++;
  53.     }
  54.     else
  55.     {
  56.         rtn = (char *) 0;
  57.     }
  58.  
  59.      while( i-- )
  60.     {
  61.         *(ptr1++) = *(ptr2++);
  62.     }
  63.     
  64.     return(rtn);
  65. }
  66.  
  67. char *
  68. memchr(ptr1,ch,len)
  69.     register char    * ptr1;
  70.     register int      ch;
  71.     int          len;
  72. {
  73.     int          i;
  74.  
  75.     for( i=0; (i < len) && (ptr1[i] != (char) ch); i++)
  76.     {
  77.     }
  78.  
  79.     malloc_check_data("memchr", ptr1, i);
  80.  
  81.     if( i < len )
  82.     {
  83.         return( ptr1+i );
  84.     }
  85.     else
  86.     {
  87.         return( (char *) 0);    
  88.     }
  89. }
  90.  
  91. char *
  92. memcpy(ptr1, ptr2, len)
  93.     register char    * ptr1;
  94.     register char    * ptr2;
  95.     register int      len;
  96. {
  97.     char        * rtn = ptr1;
  98.  
  99.     malloc_check_data("memcpy", ptr1, len);
  100.     malloc_check_data("memcpy", ptr2, len);
  101.  
  102.     /*
  103.      * while the normal memcpy does not guarrantee that it will 
  104.      * handle overlapping memory correctly, we will try...
  105.      */
  106.     if( ptr1 > ptr2  && ptr1 < (ptr2+len))
  107.     {
  108.         ptr1 += (len-1);
  109.         ptr2 += (len-1);
  110.         while( len-- > 0 )
  111.         {
  112.             *(ptr1--) = *(ptr2--);
  113.         }
  114.     }
  115.     else
  116.     {
  117.         while( len-- > 0 )
  118.         {
  119.             *(ptr1++) = *(ptr2++);
  120.         }
  121.     }
  122.     
  123.     return(rtn);
  124. }
  125.  
  126. int
  127. memcmp(ptr1, ptr2, len)
  128.     register char    * ptr1;
  129.     register char    * ptr2;
  130.     register int      len;
  131. {
  132.     malloc_check_data("memcpy", ptr1, len);
  133.     malloc_check_data("memcpy", ptr2, len);
  134.  
  135.     while( --len >= 0  && (*ptr1 == *ptr2) )
  136.     {
  137.         ptr1++;
  138.         ptr2++;
  139.     }
  140.  
  141.     /* 
  142.      * If stopped by len, return zero
  143.      */
  144.     if( len < 0 )
  145.     {
  146.         return(0);
  147.     }
  148.  
  149.     return( *ptr1 - *ptr2 );
  150. }
  151.  
  152. char * 
  153. memset(ptr1, ch, len)
  154.     register char    * ptr1;
  155.     register int      ch;
  156.     register int      len;
  157. {
  158.     char        * rtn = ptr1;
  159.  
  160.     malloc_check_data("memcpy", ptr1, len);
  161.  
  162.     while( len-- )
  163.     {
  164.         *(ptr1++) = ch;
  165.     }
  166.  
  167.     return(rtn);
  168. }
  169.  
  170. char *
  171. bcopy(ptr2,ptr1,len)
  172.     char        * ptr2;
  173.     char        * ptr1;
  174.     int          len;
  175. {
  176.     return(memcpy(ptr1,ptr2,len));
  177. }
  178.  
  179. char *
  180. bzero(ptr1,len)
  181.     char        * ptr1;
  182.     int          len;
  183. {
  184.     return(memset(ptr1,'\0',len));
  185. }
  186.  
  187. int
  188. bcmp(ptr2, ptr1, len)
  189.     char        * ptr1;
  190.     char        * ptr2;
  191.     int          len;
  192. {
  193.     return( memcmp(ptr1,ptr2,len) );
  194. }
  195.  
  196. /*
  197.  * $Log: memory.c,v $
  198.  * Revision 1.1  1992/01/24  03:29:08  dvadura
  199.  * dmake Version 3.8, Initial revision
  200.  *
  201.  * Revision 1.7  90/08/29  21:27:58  cpcahil
  202.  * fixed value of check in memccpy when character was not found.
  203.  * 
  204.  * Revision 1.6  90/07/16  20:06:26  cpcahil
  205.  * fixed several minor bugs found with Henry Spencer's string/mem tester 
  206.  * program.
  207.  * 
  208.  * 
  209.  * Revision 1.5  90/05/11  15:39:36  cpcahil
  210.  * fixed bug in memccpy().
  211.  * 
  212.  * Revision 1.4  90/05/11  00:13:10  cpcahil
  213.  * added copyright statment
  214.  * 
  215.  * Revision 1.3  90/02/24  21:50:29  cpcahil
  216.  * lots of lint fixes
  217.  * 
  218.  * Revision 1.2  90/02/24  17:29:41  cpcahil
  219.  * changed $Header to $Id so full path wouldnt be included as part of rcs 
  220.  * id string
  221.  * 
  222.  * Revision 1.1  90/02/22  23:17:43  cpcahil
  223.  * Initial revision
  224.  * 
  225.  */
  226.